package com.dang.alex.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.dang.alex.dao.DAOFactory;
import com.dang.alex.dao.LoginDAO;
import com.dang.alex.model.Book;
import com.dang.alex.model.CarItem;
import com.dang.alex.model.User;
import com.dang.alex.service.FactoryService;
import com.dang.alex.service.Userservice;
import com.dang.alex.util.Md5Code;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email=request.getParameter("email");
		String password=request.getParameter("password");
		
		FactoryService fac=new FactoryService();
		Userservice usv=fac.getUserDAO();
		Map<Integer,CarItem> s_car=new HashMap<Integer,CarItem>();
		Map<Integer,CarItem> cDel=new HashMap<Integer,CarItem>();
		User u=null;
		try {
			String psd=Md5Code.createMd5Code(password);
			u=usv.translate(email, psd);
			if(u!=null) {
				request.getSession().setAttribute("s_user",u);
				request.getSession().setAttribute("s_car",s_car);
				request.getSession().setAttribute("cDel",cDel);
				request.getRequestDispatcher("/MainServlet.do").forward(request, response);
//				book_list.jsp
			}else {
				
				request.getRequestDispatcher("/login_form.jsp").forward(request, response);
			
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
